<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NotificationController
 *
 * @author Admin
 */
class Marketing_NotificationController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > Marketing > Notifications');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $switchModel = new Application_Model_SwitchModel();
        $paginator = $switchModel->getAllCompletedSwitchesForMarketing($this->_request);
        $this->view->paginator = $paginator;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $switchModel = new Application_Model_SwitchModel();
        $paginator = $switchModel->getAllCompletedSwitchesForMarketing($this->_request);
        $this->view->paginator = $paginator;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_notification_AddForm();
        $switch_id = $this->_request->getParam('id');
        if ($this->_request->isPost()) {
            $contractPrice = $this->_request->getPost('contractPrice');
            $members = $this->_request->getPost('teamMembers');
            $users = explode(',', $members);
            $switchUsersModel = new Application_Model_SwitchUserModel();
            $switchUsersModel->saveSwitchUsers($switch_id, $users);
            $switchModel = new Application_Model_SwitchModel();
            $data = array();
            $data['id'] = $switch_id;
            $data['contract_price'] = $contractPrice;
            $switchModel->save($data);
        }
        $this->view->form = $form;
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_notification_AddForm();
        $switchUsersModel = new Application_Model_SwitchUserModel();
        $switchModel = new Application_Model_SwitchModel();
        $switchId = $this->_request->getParam('id');
        $switchDetail = $switchModel->getSwitch('id', $switchId);
        $form->populate(array(
            'contractPrice' => $switchDetail['contract_price']
        ));
        if ($this->_request->isPost()) {
            $contractPrice = $this->_request->getPost('contractPrice');
            $members = $this->_request->getPost('teamMembers');
            $users = explode(',', $members);
            $switchUsersModel->saveSwitchUsers($switchId, $users);
            $data = array();
            $data['id'] = $switchId;
            $data['contract_price'] = $contractPrice;
            $switchModel->save($data);
        }
        $this->view->form = $form;
    }

    public function housesAction() {
        $this->_helper->layout->disableLayout();
        $switchId = $this->_request->getParam('id');
        $houseModel = new Application_Model_HouseModel();
        $switchUserModel = new Application_Model_SwitchUserModel();

        $users = $switchUserModel->getAllSwitchUser($switchId);
        $houses = $houseModel->getAllHouseDetail('switch', $switchId);

        $this->view->users = $users;
        $this->view->houses = $houses;
    }

    public function assignhousesAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $houseId = $this->_request->getParam('id');
        $userId = $this->_request->getParam('page');

        $houseModel = new Application_Model_HouseModel();

        $check = $this->_request->getPost('check');
        if ($check) {
            $houseModel->update(array('marketing_user_id' => $userId), "id = '$houseId'");
        } else {
            $houseModel->update(array('marketing_user_id' => 0), "id = '$houseId'");
        }
    }

    public function showfilesAction() {
        $this->_helper->layout->disableLayout();
        $switchId = $this->_request->getParam('id');
        $documentModel = new Application_Model_DocumentModel();

        $docs = $documentModel->getAllDocument('switch', $switchId);
        $this->view->files = $docs;
        $this->view->switchId = $switchId;
    }

}